package com.yqRecite.controller;

import com.yqRecite.service.AdminService;
import com.yqRecite.service.UserService;
import com.yqRecite.entity.*;
import com.yqRecite.util.result.ResultUtil;
import com.yqRecite.vo.AllPeople;
import com.yqRecite.util.result.Result;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static org.ietf.jgss.GSSException.FAILURE;

@RestController
@CrossOrigin
@RequestMapping("admin")
public class AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    private UserService userService;

    @RequestMapping("register")
    public Result register(@RequestBody Admin admin){
        System.out.println("admin: " + admin);
        admin.setAvatar("../../static/pig.jpg");
        admin.setRole("admin");
        return adminService.addAdmin(admin);
    }

    @RequestMapping("getAllPeople")
    Result getAllPeople(){
        List<User> allPeopleList = userService.getAllUserList();
        if (allPeopleList != null){
            return ResultUtil.ok(allPeopleList);
        }
        return ResultUtil.ok(allPeopleList);
    }

    /**
     * 生成测试用户数据
     * @param quantity
     * @return
     */
    @RequestMapping("generateRandomUsers/{quantity}")
    Result generateRandomUsers(@PathVariable("quantity") int quantity){
        for (int i = 0; i < quantity; i++) {
            User user = new User();
            user.setUsername(RandomStringUtils.randomAlphabetic(8));
            user.setPassword(RandomStringUtils.randomAlphanumeric(10));
            user.setPhone(RandomStringUtils.randomNumeric(10));
            user.setEmail(RandomStringUtils.randomAlphabetic(8) + "@hzp.com");
            user.setGender((Math.random() < 0.5) ? "男" : "女");
            user.setRole("user");
            user.setAvatar("https://hzp.com/avatar.jpg");
            user.setCreateTime(new Date());
            user.setEnable(1);
            Result res = userService.addOneUser(user);
            if (res.getCode() != 200){
                List<User> userList = userService.getAllUserList();
                String desc = "数据生成中断，已生成【" + (i+1) + "】条数据";
                ResultUtil.fail(desc, userList);
            }
        }
        List<User> userList = userService.getAllUserList();
        return ResultUtil.ok(userList);
    }

    @RequestMapping("addPeople")
    public Result addPeople(@RequestBody AllPeople allPeople){
        System.out.println("allPeople: " + allPeople);
        allPeople.setRole("user");
        User person = new User();
        person.setUsername(allPeople.getUsername());
        person.setCreateTime(allPeople.getCreateTime());
        person.setPassword(allPeople.getPassword());
        person.setAvatar("../../static/plane.png");
        person.setCreateTime(new Date());
        person.setGender(allPeople.getGender());
        person.setEmail(allPeople.getEmail());
        person.setEnable(1);
        person.setPhone(allPeople.getPhone());
        return userService.addOneUser(person);
    }

    @RequestMapping("savePassword")
    Result savePassword(@RequestBody Admin user){
        System.out.println("user==>updatePwd==>"+user.toString());
        return adminService.updateAdmin(user);
    }


    @PostMapping("deletePeople")
    Result deletePeople(@RequestBody AllPeople allPeople){
        System.out.println("allPeople: " + allPeople);
            int res = userService.deleteUser(allPeople.getId());
            if(res == 1){
                return ResultUtil.ok(allPeople);
            }else{
                return ResultUtil.fail("删除失败", allPeople);
            }
    }

}
